<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_Kategori extends CI_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	function getAllKategori(){
		$query = $this->db->get('fa_kategori');
		if($query->num_rows() > 0){
			return $query->result_array();
		}else{
			return array();
		}
	}

	function getKategoriByRoot($strParam){
		$strSQL = "SELECT * FROM fa_kategori WHERE root=?";
		$strQuery = $this->db->query($strSQL, $strParam);
		if($strQuery->num_rows() > 0){
            foreach ($strQuery->result_array() as $arrRow) {
                $arrStrResult[] = $arrRow;
            }
            $strQuery->free_result();
            return $arrStrResult;
        }else{
            return array();
        }
	}

	function getSortedKategori(){
		$rootKategori = $this->getKategoriByRoot("root");
		foreach ($rootKategori as $key => $root) {
			$rootKategori[$key]["childs"] = $this->getKategoriByRoot($root["id_kategori"]);
		}
		return $rootKategori;
	}

	function getOneById($strId){
		$strSQL = "SELECT * FROM tb_kategori WHERE id_kategori = ?";
		$strQuery = $this->db->query($strSQL, $strId);
		if($strQuery->num_rows() > 0){
            $arrResult = $strQuery->row_array();
            $strQuery->free_result();
            return $arrResult;
        }else{
            return array();
        }
	}

	function insertKategori($arrParam){
		$boolStatus = FALSE;
		$strSQL = "INSERT INTO tb_kategori(id_kategori,root,nama_kategori) VALUES(?,?,?)";

        if($this->db->query($strSQL, $arrParam)){
        	$boolStatus = TRUE;
        }else{
            $boolStatus = FALSE;
        }
		return $boolStatus;
	}

	function updateKategori($arrParam){
		$boolStatus = FALSE;
		$strSQL = "UPDATE tb_kategori SET root = ?, nama_kategori = ? WHERE id_kategori = ?";
        if($this->db->query($strSQL, $arrParam)){
        	$boolStatus = TRUE;
        }else{
            $boolStatus = FALSE;
        }
		return $boolStatus;
	}

	function deleteKategori($strId){
		$boolStatus = FALSE;
		$strSQL = "DELETE FROM tb_kategori WHERE id_kategori = ?";
        if($this->db->query($strSQL, $strId)){
        	$boolStatus = TRUE;
        }else{
            $boolStatus = FALSE;
        }
		return $boolStatus;
	}
}
/* End of file m_kategori.php */
/* Location: .models/m_kategori.php */